package flca.voicenav.util;

import android.speech.tts.TextToSpeech;
import android.util.Log;
import flca.voicenav.VoiceNavConstants;
import flca.voicenav.model.GpxRoute;
import flca.voicenav.model.TracePt;
import flca.voicenav.model.TrkPt;
import flca.voicenav.model.VoiceNavData;

public class SpeechHelper implements VoiceNavConstants
{
	private int informCount = 0;
	private String sayAngle;
	private int nextCoord = -1;
	private TextToSpeech mTts;
	
	public SpeechHelper(TextToSpeech aTts) {
		super();
		this.mTts = aTts;
	}

	public void update(TracePt aTracePt) 
	{
		if (nextCoord != aTracePt.getNextCorner()) {
			nextCoord = aTracePt.getNextCorner();
			informCount = 0;
			sayAngle = getAngleAsString();
		}
	}
	
	public void inform(int aDistance) 
	{
		if (informCount < 1) {
			say( "in " + rounded(aDistance) + " meters " +  sayAngle);
		}
		informCount++;
	}
	
	public void informFinish(int aDistance) 
	{
		if (informCount < 1) {
//			SoundMgr.getInstance().playSound(SND_HAPPY);
//			say( "you reach the finish in " + rounded(aDistance) + " meters " +  sayAngle);
		}
		informCount++;
	}
	
	private void say(String aMsg) {
		if (aMsg != null && aMsg.trim().length() > 0) {
			mTts.speak(aMsg, TextToSpeech.QUEUE_FLUSH, null);
		} else {
			Log.w(TAG, "mts was null");
		}
	}
	
 	private String getAngleAsString()
	{
 		if (corrtTrkPt() != null) {
 			double angle = corrtTrkPt().getAngle();
 	 		return GetNaturalDirection.getAngleAsString(angle);
 		} else {
 			return "";
 		}
	}

	private int rounded(double aValue) {
		return ((int) Math.round(aValue/10.0)) * 10;
	}

	private int rounded(int aValue) {
		return rounded( (double) aValue);
	}
		
	//--shortcuts
	private GpxRoute gpxRoute() {
		return VoiceNavData.gpxRoute;
	}
	
	private TrkPt corrtTrkPt() 
	{
		if (nextCoord >= 0 && nextCoord < gpxRoute().size() - 1) {
			return gpxRoute().getCoordinates().get(nextCoord);
		} else {
			return null;
		}
	}
	
}
